package com.das.mechanic_base.adapter.common;

import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.CarBrandBean;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class X3SelectStyleAdapter extends RecyclerView.Adapter<X3SelectStyleAdapter.SelectStyleHolder> {

    private List<CarBrandBean> mList;

    private Context mContext;
    boolean isFilter;
    String key;

    public X3SelectStyleAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();
    }

    public void changeData(List<CarBrandBean> mList) {
        this.mList = mList;
        notifyDataSetChanged();
    }

    public void changeData(List<CarBrandBean> mList, boolean isFilter, String key) {
        this.mList = mList;
        this.isFilter = isFilter;
        this.key = key;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public SelectStyleHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SelectStyleHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_select_style_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull SelectStyleHolder holder, int position) {
        if (mList == null || mList.size() - 1 < position || X3StringUtils.isEmpty(mList.get(position).getLabel())) {
            return;
        }
        if (isFilter) {
            String label = mList.get(position).getLabel();
            if (!X3StringUtils.isEmpty(key)) {
                char[] nameArr = mList.get(position).getLabel().toLowerCase().toCharArray();
                char[] keyArr = key.toCharArray();
                List<Integer> indexList = new ArrayList<>();
                for (int i = 0; i < keyArr.length; i++) {
                    for (int k = 0; k < nameArr.length; k++) {
                        if (keyArr[i] == nameArr[k]) {
                            if (!indexList.contains(k)) {
                                indexList.add(k);
                            }
                        }
                    }
                }
                SpannableString spanString = new SpannableString(label);
                if (indexList != null && indexList.size() > 0) {
                    for (int i = 0; i < indexList.size(); i++) {
                        ForegroundColorSpan span = new ForegroundColorSpan(Color.parseColor("#0077FF"));
                        spanString.setSpan(span, indexList.get(i), indexList.get(i) + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                }
                holder.tv_name.setText(spanString);
            } else {
                holder.tv_name.setText(label);
            }

        } else {
            holder.tv_name.setText(mList.get(position).getLabel() + "");
        }

        holder.itemView.setOnClickListener(view -> {
            if (iOnSelectStyle != null) {
                iOnSelectStyle.iOnSelectStyleName(mList.get(position));
            }
        });
    }

    public boolean isGroupHead(int position) {
        if (position >= mList.size()) {
            return false;
        }
        if (position == 0) {
            return true;
        } else {
            String currentGroup = getGroupName(position);
            String preGroup = getGroupName(position - 1);
            return !currentGroup.equals(preGroup);
        }
    }

    /**
     * 获取组名
     */
    public String getGroupName(int position) {

        String sortLetters = mList.get(position).getSortLetters();

        return "🔥".equals(sortLetters) ? mContext.getString(R.string.x3_common_brand) : sortLetters;
    }

    IOnSelectStyle iOnSelectStyle;

    public void setiOnSelectStyle(IOnSelectStyle iOnSelectStyle) {
        this.iOnSelectStyle = iOnSelectStyle;
    }

    public interface IOnSelectStyle {
        void iOnSelectStyleName(CarBrandBean brandBean);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class SelectStyleHolder extends RecyclerView.ViewHolder {
        TextView tv_name;

        public SelectStyleHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
        }
    }
}
